import matplotlib.pyplot as plt
import numpy as np

# 设置支持 Unicode 的字体
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

def demonstrate_continuity_in_limit_calculation():
    print("连续函数在极限计算中的应用示例:")
    print("=" * 50)
    
    # 示例1：多项式函数的极限
    def poly_func(x):
        return x**3 - 2*x**2 + 3*x - 1
    
    x0 = 2
    limit_value = poly_func(x0)
    print(f"例1: lim(x→{x0}) (x³ - 2x² + 3x - 1)")
    print(f"由于多项式函数在x={x0}处连续，直接代入:")
    print(f"极限值 = {limit_value}")
    print()
    
    # 示例2：三角函数的极限
    def trig_func(x):
        return np.sin(x) / (x + 1)  # 在x=0处连续
    
    x0 = 0
    limit_value = trig_func(x0)
    print(f"例2: lim(x→{x0}) sin(x)/(x + 1)")
    print(f"由于函数在x={x0}处连续，直接代入:")
    print(f"极限值 = {limit_value}")
    print()
    
    # 示例3：复合函数的极限
    def composite_func(x):
        return np.log(1 + x**2)  # 在x=1处连续
    
    x0 = 1
    limit_value = composite_func(x0)
    print(f"例3: lim(x→{x0}) ln(1 + x²)")
    print(f"由于复合函数在x={x0}处连续，直接代入:")
    print(f"极限值 = {limit_value}")
    print()
    
    # 可视化验证
    x = np.linspace(-1, 3, 1000)
    
    plt.figure(figsize=(15, 4))
    
    plt.subplot(1, 3, 1)
    y1 = poly_func(x)
    plt.plot(x, y1, 'b-', linewidth=2)
    plt.scatter([x0], [limit_value], color='red', s=50)
    plt.title(f'lim(x→{x0}) (x³ - 2x² + 3x - 1) = {limit_value}')
    plt.grid(True)
    
    plt.subplot(1, 3, 2)
    y2 = trig_func(x)
    plt.plot(x, y2, 'g-', linewidth=2)
    plt.scatter([x0], [trig_func(x0)], color='red', s=50)
    plt.title(f'lim(x→{x0}) sin(x)/(x+1) = {trig_func(x0):.4f}')
    plt.grid(True)
    
    plt.subplot(1, 3, 3)
    y3 = composite_func(x)
    plt.plot(x, y3, 'purple', linewidth=2)
    plt.scatter([x0], [composite_func(x0)], color='red', s=50)
    plt.title(f'lim(x→{x0}) ln(1+x²) = {composite_func(x0):.4f}')
    plt.grid(True)
    
    plt.tight_layout()
    plt.show()

demonstrate_continuity_in_limit_calculation()